using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for HtmlTable
/// </summary>
public class MakeHtmlTable
{
    static private int MaxDisplayRows = 20;

	public MakeHtmlTable()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    static public void makeHTMLTable(bool detail, Panel AllPagesArea, int maxDisplayRowCount, Data.Data currentData, Data.VarParams varParam )
    {
        AllPagesArea.Controls.Clear();

        HtmlTable overallTable = new HtmlTable();   // each page is a row in this table
        overallTable.Width = "100%";

        AllPagesArea.Controls.Add(overallTable);

        Data.Page currentPage = currentData.GetSelectedPage();

        for (int pageIndex = 0, pageCount = currentData.GetPageCount(); pageIndex < pageCount; pageIndex++)
        {
            Data.Page thisPage = currentData.GetPage(pageIndex);
            if (thisPage == null) return;

            if (thisPage.Visible())
            {
                HtmlTableRow overallThisRow = new HtmlTableRow();
                overallTable.Controls.Add(overallThisRow);
                HtmlTableCell overallThisCell = new HtmlTableCell();
                overallThisRow.Controls.Add(overallThisCell);

                Label label = new Label();
                label.Attributes["class"] = "PageCaption";
                label.Text = thisPage.GetPageCaption(); ;
                overallThisCell.Controls.Add(label);

                string projectDescription = thisPage.GetPageDescription();
                if (projectDescription.Length > 0)
                {
                    label = new Label();
                    label.Attributes["class"] = "PageSubCaption";
                    label.Text = "<br />" + projectDescription;
                    overallThisCell.Controls.Add(label);
                }

                HtmlTable thisPageCrosstab = new HtmlTable();
                thisPageCrosstab.Attributes["class"] = "DataTable";
                thisPageCrosstab.Attributes["cellspacing"] = "1";
                thisPageCrosstab.Attributes["width"] = "100%";

                overallThisCell.Controls.Add(thisPageCrosstab);

                thisPageCrosstab.Rows.Clear();
                makeHeaderRow(thisPageCrosstab, thisPage);

                for (int rowIndex = 0, rowCount = thisPage.GetRowCount(); 
                    rowIndex <  MaxDisplayRows && 
                    rowIndex < maxDisplayRowCount && rowIndex < rowCount; rowIndex++)
                {
                    makeRow(thisPageCrosstab, thisPage, rowIndex, varParam );
                }

            }
        }
    }

    static private void makeHeaderRow( HtmlTable htmlTable, Data.Page currentPage )
    {
        HtmlTableRow headerRow = new HtmlTableRow();
        headerRow.Align = "center";
        htmlTable.Rows.Add(headerRow);

        // row label "name"
        {
            HtmlTableCell thisCell = new HtmlTableCell();
            thisCell.InnerHtml = currentPage.ccs.GetRowLabelName();
            thisCell.Attributes["class"] = "RowLabelCaption";
            headerRow.Controls.Add(thisCell);
        }

        // visible text column headers
        for (int i = 0, count = currentPage.GetTextColumnCount(); i < count; i++)
        {
            HtmlTableCell thisCell = new HtmlTableCell();
            thisCell.InnerHtml = currentPage.GetTextColumnName(i);
            thisCell.Attributes["class"] = "TextColumnCaption";
            headerRow.Controls.Add(thisCell);
        }

        // column headers
        for (int colIndex = 0, colCount = currentPage.GetNumericColumnCount(); colIndex < colCount; colIndex++)
        {
            HtmlTableCell thisCell = new HtmlTableCell();
            thisCell.InnerHtml = currentPage.GetNumericColumnName(colIndex);
            thisCell.Attributes["class"] = "ColCaption";
            headerRow.Controls.Add(thisCell);
        }
    }

    static private void makeRow(HtmlTable htmlTable, Data.Page currentPage, int rowIndex, Data.VarParams varParam )
    {
        HtmlTableRow thisRow = new HtmlTableRow();
        thisRow.Align = "center";
        htmlTable.Rows.Add(thisRow);

        HtmlTableCell thisCell = new HtmlTableCell();

        {
            // row-label
            string ccCaption = currentPage.GetRowLabelValue(rowIndex);
#if WANT_ENHANCED_CAPTION
            if (currentData.Pages().Count() > 1)
                thisCell.InnerHtml = currentPage.Caption() + " " + ccCaption;
            else
#endif
            thisCell.InnerHtml = ccCaption;

            thisCell.Attributes["class"] = "RowLabel";
            thisRow.Controls.Add(thisCell);

            for (int i = 0, count = currentPage.GetTextColumnCount(); i < count; i++)
            {
                thisCell = new HtmlTableCell();
                thisCell.InnerHtml = currentPage.GetTextColumnValue(rowIndex, i);
                thisCell.Attributes["class"] = "TextColumn";
                thisRow.Controls.Add(thisCell);
            }

            // data columns
            for (int colIndex = 0, colCount = currentPage.GetNumericColumnCount(); colIndex < colCount; colIndex++)
            {
                makeTD(thisRow, rowIndex, colIndex, currentPage, varParam );
            }
        }
    }

    static private void makeTD(HtmlTableRow htmlRow, int rowIndex, int colIndex, Data.Page currentPage, Data.VarParams varParam)
    {
        HtmlTableCell thisCell = new HtmlTableCell();

        String innerText = currentPage.GetNumericCellContents(rowIndex, colIndex, true );
        thisCell.InnerHtml = innerText;
        thisCell.Attributes["class"] = currentPage.GetCssClass(rowIndex, colIndex);  

        htmlRow.Controls.Add(thisCell);
    }
}
